package com.zxmr.hospital.customer.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Opportunity;
import com.zxmr.domain.account.AccountType;
import com.zxmr.domain.service.OpportunitySearchBean;
import com.zxmr.domain.service.OpportunityService;
import com.zxmr.hospital.customer.dto.JSONListDTO;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.Utils;

public class QueryAllOpportunity extends Executor{

	@Override
	public void execute(Request request, Response response) {
		

		try
		{
			queryAll(request,response);

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}
	
	private void queryAll(Request request,Response response)throws ExecuteException
	{
		OpportunitySearchBean searchBean = setSearchBean(request);
		if(request.getAccount().getAccountType() != AccountType.Supervisor)
		{
		
			if(!searchBean.getOwner().equals(request.getAccount().getName()))
			{
				if(!this.isAuthorized(request.getAccount().getId(), "queryOpportunityOthers"))
					throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.noauth.queryOpportunityOthers"));
			}
		}
		
		JSONListDTO list = query(searchBean);
		reply(response,list);
	}
	private JSONListDTO query(OpportunitySearchBean searchBean)
	{
		OpportunityService service = new OpportunityService();
		
		List<Opportunity> opps = service.queryOpportunityPagination(searchBean);
		int total = service.queryOpportunityCount(searchBean);
		
		JSONListDTO list = new JSONListDTO();
		list.setTotal(total);
		list.setRows(opps);
		return list;
	}
	private OpportunitySearchBean setSearchBean(Request request)
	{
		int count = getCountPerPage(request);
		int start = getCountStart(request);
		
		OpportunitySearchBean searchBean = new OpportunitySearchBean();
		searchBean.setCompany(request.getAccount().getCompany());
		searchBean.setLimit(count);
		searchBean.setStart(start);

		String strategy = request.getRequestParameters().get("strategy");
		String items = request.getRequestParameters().get("items");
		String employee = request.getRequestParameters().get("employee");
		String from = request.getRequestParameters().get("from");
		String to = request.getRequestParameters().get("to");
		
		searchBean.setStrategy(strategy);
		searchBean.setItems(items);
		
		if(Utils.isStringEmptyWithTrim(employee))
			searchBean.setOwner(request.getAccount().getName());
		else if("*".equals(employee))
			searchBean.setOwner("");
		else
			searchBean.setOwner(employee);
		
		searchBean.setFromTime(from);
		searchBean.setToTime(to);
		
		return searchBean;
	}
	private int getCountPerPage(Request request)
	{
		String count = request.getRequestParameters().get("size");
		if(count == null || "".equals(count.trim()))return 10;
		try
		{
			return Integer.parseInt(count);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 10;
		}
	}
	private int getCountStart(Request request)
	{
		String start = request.getRequestParameters().get("start");
		if(start == null || "".equals(start.trim()))return 0;
		try
		{
			return Integer.parseInt(start);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	private void reply(Response response,JSONListDTO dto)
	{
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(dto));
		response.setPayload(payload);
	}
}
